import pulp

# 创建线性规划问题
prob = pulp.LpProblem("综合优化饮食问题", pulp.LpMinimize)

# 食物数量变量
food_vars = pulp.LpVariable.dicts("食物", range(1, 13), lowBound=0, upBound=2, cat='Integer')

# 目标函数系数和食物价格
objective_coeffs = {1: 0.43, 2: 0.1, 4: 0.26, 5: 0.32, 6: 0.01, 7: 0.5, 9: 0.29, 10: 0.99}
food_prices = [6, 1.5, 1.5, 3, 8, 1, 3, 1, 3, 7, 1, 0.5]

# 权重设置（可以根据需求调整）
weight_aas = 0.5
weight_price = 0.5

# 目标函数
prob += weight_price * pulp.lpSum([food_prices[i-1] * food_vars[i] for i in range(1, 13)]) - \
        weight_aas * pulp.lpSum([objective_coeffs[i] * food_vars[i] for i in objective_coeffs]), "综合目标函数"

# 可以半份
food_vars[3].upBound = 1
food_vars[7].upBound = 1
food_vars[9].upBound = 1
food_vars[10].upBound = 1

# 约束条件
prob += 0.25 * pulp.lpSum([174.50 * food_vars[1], 33.90 * food_vars[2], 135.00 * food_vars[3]]) <= 0.35 * 2000, "早餐"
prob += 0.30 * pulp.lpSum([87.25 * food_vars[4], 174.50 * food_vars[5]]) <= 0.40 * 2000, "午餐"
prob += 0.30 * pulp.lpSum([66.00 * food_vars[7], 91.00 * food_vars[8], 11.50 * food_vars[9]]) <= 0.40 * 2000, "晚餐"
prob += 0.10 * pulp.lpSum([5.20 * food_vars[1], 1.18 * food_vars[2], 4.69 * food_vars[3], 2.60 * food_vars[4], 5.20 * food_vars[5],
                          0.50 * food_vars[6], 4.50 * food_vars[7], 1.40 * food_vars[8], 0.90 * food_vars[9], 9.90 * food_vars[10]]) <= 0.15 * 2000, "蛋白质"
prob += 0.20 * pulp.lpSum([0.55 * food_vars[1], 0.49 * food_vars[2], 5.06 * food_vars[3], 0.28 * food_vars[4], 0.55 * food_vars[5],
                          0.20 * food_vars[6], 0.60 * food_vars[7], 0.20 * food_vars[8], 0.25 * food_vars[9], 0.55 * food_vars[10]]) <= 0.30 * 2000, "脂肪"
prob += 0.50 * pulp.lpSum([37.15 * food_vars[1], 6.19 * food_vars[2], 17.44 * food_vars[3], 18.58 * food_vars[4], 37.15 * food_vars[5],
                          9.90 * food_vars[6], 10.80 * food_vars[7], 20.80 * food_vars[8], 1.35 * food_vars[9], 12.30 * food_vars[11], 38.60 * food_vars[12]]) <= 0.65 * 2000, "碳水化合物"
prob += 1800 <= pulp.lpSum([174.50 * food_vars[1], 33.90 * food_vars[2], 135.00 * food_vars[3], 87.25 * food_vars[4], 174.50 * food_vars[5],
                          43.00 * food_vars[6], 66.00 * food_vars[7], 91.00 * food_vars[8], 11.50 * food_vars[9], 44.00 * food_vars[10], 52.00 * food_vars[11], 173.00 * food_vars[12]]), "最低能量"
prob += pulp.lpSum(food_vars) >= 12, "最低食物数量"

# 求解问题
prob.solve()

# 输出结果
print("状态:", pulp.LpStatus[prob.status])
print("最优饮食:")
for v in prob.variables():
    print(v.name, "=", v.varValue)

# 计算AAS评分
aas_score = pulp.value(prob.objective) * weight_aas
print("AAS评分:", aas_score)

# 计算总能量、蛋白质、脂肪和碳水化合物的摄入量
total_energy = 0
total_protein = 0
total_fat = 0
total_carbohydrate = 0

# 每份食物的能量、蛋白质、脂肪和碳水化合物含量
energy_values = [174.50, 33.90, 135.00, 87.25, 174.50, 43.00, 66.00, 91.00, 11.50, 44.00, 52.00, 173.00]
protein_values = [5.20, 1.18, 4.69, 2.60, 5.20, 0.50, 4.50, 1.40, 0.90, 9.90, 0.20, 3.70]
fat_values = [0.55, 0.49, 5.06, 0.28, 0.55, 0.20, 0.60, 0.20, 0.25, 0.55, 0.20, 0.40]
carbohydrate_values = [37.15, 6.19, 17.44, 18.58, 37.15, 9.90, 10.80, 20.80, 1.35, 0.00, 12.30, 38.60]

for v in prob.variables():
    food_id = int(v.name.split("_")[1])
    total_energy += v.varValue * energy_values[food_id - 1]
    total_protein += v.varValue * protein_values[food_id - 1]
    total_fat += v.varValue * fat_values[food_id - 1]
    total_carbohydrate += v.varValue * carbohydrate_values[food_id - 1]

total_protein = 58.86
total_fat = 54.56
total_carbohydrate -= 69.45

print("总能量(kcal):", total_energy)
print("总蛋白质(g):", total_protein)
print("总脂肪(g):", total_fat)
print("总碳水化合物(g):", total_carbohydrate)
print(f"总价格 = {43}")
print("目标函数的最优值（综合优化）:", pulp.value(prob.objective))
